package com.mapgis

import com.mapgis.constant.Constants
import com.mapgis.service.{EventParseService, SqlBox}
import com.mapgis.utils.{DateUtils, JDBCUtil}

import scala.collection.mutable

object DataMergeinto {

    def main(args: Array[String]): Unit = {
        //拼接mergeinto语句并执行
        if (args(0)!="event") {
            commonMergeInto(args(0))
        }else if(args(0)=="event"){
            eventMergeInto
        }

    }

    /**
      * 通过表名称选取对应的中间临时表名称
      * @param params
      * @return
      */
    def getTmpTableByParams(params: String) ={
        var lgTmpTable=""
        params match {
            case "building" => lgTmpTable=Constants.TMP_LG_BUILDING
            case "house" => lgTmpTable=Constants.TMP_LG_HOUSE
            case "resident" => lgTmpTable=Constants.TMP_LG_RESIDENT
            case "residenthmt" => lgTmpTable=Constants.TMP_LG_RESIDENTHMT
            case "residenthmtlogout" => lgTmpTable=Constants.TMP_LG_RESIDENTHMT_L
            case "residentoutside" => lgTmpTable=Constants.TMP_LG_RESIDENTOUTSIDE
            case "residentoutsidelogout" => lgTmpTable=Constants.TMP_LG_RESIDENTOUTSIDE_L
            case "residentlogout" => lgTmpTable=Constants.TMP_LG_RESIDENTLOGOUT
            case "enterprise" => lgTmpTable=Constants.TMP_LG_ENTERPRISEINFO
            case "enterprisemap" => lgTmpTable=Constants.TMP_LG_ENTERPRISEINFOMAP
        }
        lgTmpTable
    }

    /**
      * 通过表名称选取对应的目标表名称
      * @param params
      * @return
      */
    def getTargetTableByParams(params: String) ={
        var lgTable = ""
        params match {
            case "building" => lgTable=Constants.TABLE_LG_BUILDING
            case "house" => lgTable=Constants.TABLE_LG_HOUSE
            case "resident" => lgTable=Constants.TABLE_LG_RESIDENT
            case "residenthmt" => lgTable=Constants.TABLE_LG_RESIDENT
            case "residentoutside" => lgTable=Constants.TABLE_LG_RESIDENT
            case "residenthmtlogout" => lgTable=Constants.TABLE_LG_RESIDENTLOGOUT
            case "residentoutsidelogout" => lgTable=Constants.TABLE_LG_RESIDENTLOGOUT
            case "residentlogout" => lgTable=Constants.TABLE_LG_RESIDENTLOGOUT
            case "enterprise" => lgTable=Constants.TABLE_LG_ENTERPRISEINFO
            case "enterprisemap" => lgTable=Constants.TABLE_LG_ENTERPRISEINFOMAP
        }
        lgTable
    }

    def getEventUpdateNum() ={
        val sql = JDBCUtil.leftJoin("count(*) count",Constants.TMP_LG_EVENT,Constants.TABLE_LG_IMP_EVENT,"a.id=b.id","b.id is null")
        val res = JDBCUtil.handExecuteQuery(sql)
        val count = res.map(_.get("COUNT")).head
        count
    }

    def getEventUpdateIDList() ={
        val resMap = new mutable.HashMap[String,Int]()
        val sql = JDBCUtil.leftJoin("a.id as id",Constants.TMP_LG_EVENT,Constants.TABLE_LG_IMP_EVENT,"a.id=b.id","b.id is null")
        val res = JDBCUtil.handExecuteQuery(sql)
        val idList = res.map(_.get("id".toUpperCase))
        idList.foreach{resMap.put(_,1)}
        resMap
    }

    /**
      * 拼接对应的merge语句，并执行
      * @param jobName
      */
    def commonMergeInto(jobName: String): Unit ={
        var mergeNum = 0
        val lgTmpTable = getTmpTableByParams(jobName)
        val lgTable = getTargetTableByParams(jobName)
        val usingSql = new SqlBox().getSqlByJobName(jobName)     //获取对应的merge语句
        val insertFieldList = JDBCUtil.getFieldFromTableName(lgTable)
        val updateFieldList = insertFieldList.filter(_.toLowerCase!="id")
        val count = JDBCUtil.getNumTable(lgTmpTable)
        println("导入开始时间:" + DateUtils.getLocalDateTime())

        if (count!=0) {
            var mergeCondition: String=null
            jobName match {
//                case "resident" => mergeCondition=List("t1.id=t2.id")
//                case "residenthmt" => mergeCondition=List(2+"id")
//                case "residentoutside" => mergeCondition=List(3+"id"+"id")
                case _ => mergeCondition="t1.id=t2.id"
            }
            mergeNum = JDBCUtil.mergeInto(lgTable,usingSql,mergeCondition,updateFieldList,insertFieldList)
        }
        println("导入结束时间:" + DateUtils.getLocalDateTime())
        println("导入数据数量:" + mergeNum) //先观察这个值与count的差距大不大
    }

    def eventMergeInto(): Unit ={

        val oriEventList = JDBCUtil.handExecuteQuery(new SqlBox().getSqlByJobName("event"))
        println("解析事件开始时间:" + DateUtils.getLocalDateTime())
        val impEventList = EventParseService.getImpEventFromSqlRes(oriEventList)
        println("解析事件结束时间:" + DateUtils.getLocalDateTime())
        println("导入事件临时表的时间:" + DateUtils.getLocalDateTime())
        var shouldEventImportMap=getEventUpdateIDList()
        var eventImportNum = 0;
        var peopleImportNum = 0;
        if (eventImportNum!="0"){
            impEventList.filter{impEvent=>shouldEventImportMap.contains(impEvent.id)}.foreach{
                impEvent =>{
                    eventImportNum = eventImportNum + JDBCUtil.mergeEvent(Constants.TABLE_LG_IMP_EVENT,"id",impEvent)
                    println("导入第n条数据的时间:" + eventImportNum + "---->" + DateUtils.getLocalDateTime())
                }
            }
//            eventImportNum = JDBCUtil.mergeEvent(Constants.TABLE_LG_IMP_EVENT,"id",impEventList.filter{impEvent=>shouldEventImportMap.contains(impEvent.id)})

            val impPeopleList = EventParseService.getImpPeopleFromSql(oriEventList)
            println("导入诉求人员表的时间:" + DateUtils.getLocalDateTime())
//            impPeopleList.foreach{
//                impPeople=>{
////                    val insertList = JDBCUtil.getFieldFromTableName(Constants.TABLE_LG_IMP_PERSON)
////                    val updateList = insertList.filter(x => x.toLowerCase!="id" && x.toLowerCase!="isread")
//
//                    peopleImportNum = peopleImportNum + JDBCUtil.mergePeople(Constants.TABLE_LG_IMP_PERSON,"id",impPeople)
//                    println("导入第n条人员信息的时间:" + peopleImportNum + "---->" + DateUtils.getLocalDateTime())
//                }
//            }
            val peopleImportNum = JDBCUtil.mergePeople(impPeopleList)
        }
        println("应导入事件数量:" + shouldEventImportMap.size)
        println("完成导入事件数量:" + eventImportNum)
    }


}
